package ytmc.converter;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.configuration.XMLConfiguration;
import ytmc.setup.*;
import ytmc.Controller;
import ytmc.FileUtils;
import ytmc.View;


/**
 *
 * @author Kicok
 */
public class ConverterController implements Controller
{
    private ConverterModel model;
    private ConverterView view;

    public ConverterController()
    {
        this.model = new ConverterModel();
        this.view = new ConverterView( this, model );
    }


    public void addTask()
    {
        model.createTask( view.getInputFilePath(), view.getOutputFilePath() );
    }

    public void removeTask( int id )
    {
        model.removeTask( id );
    }

    public void removeTasks( int[] ids )
    {
        for( int i = ids.length - 1; i >= 0; i-- )
        {
            removeTask( ids[i] );
        }
    }

    public void startTask( int id )
    {
        try
        {
            model.startTask( id );
        }
        catch( Exception e )
        {
            view.showMessage( e.getMessage() );
        }
    }

    public void startTasks( int[] ids )
    {
        for( int id : ids )
        {
            startTask( id );
        }
    }


    public void createOutputFilePath()
    {
        if( view.getInputFilePath().isEmpty() )
        {
            view.setOutputFilePath( "" );
            return;
        }

        int outputFileIndex = 0;
        File inputFile = new File( view.getInputFilePath() );
        String fileName = FileUtils.getFileName( inputFile );
        String extension = getOutputExtension( inputFile );

        if( extension.equals( FileUtils.getExtension( inputFile ).toLowerCase() ) )
        {
            outputFileIndex++;
        }

        List<String> taskFiles = new ArrayList<String>();
        List<ConversionTask> tasks = model.getTasks();
        for( ConversionTask task : tasks )
        {
            taskFiles.add( task.getOutputFile().getAbsolutePath() );
        }

        String outputFilePath;
        do
        {
            outputFilePath = fileName;
            if( outputFileIndex > 0 )
            {
                outputFilePath += "(" + outputFileIndex + ")";
            }
            outputFilePath += "." + extension;

            outputFileIndex++;
        }
        while( new File( outputFilePath ).exists() || taskFiles.contains( outputFilePath ) );

        view.setOutputFilePath( outputFilePath );
    }

    private String getOutputExtension( File inputFile )
    {
        XMLConfiguration config = Config.load();

        if( view.isVideoConversion() )
        {
            return ConfigUtils.getEnum( config, "video-format", VideoFormat.DEFAULT_FORMAT ).getFriendlyName();
        }
        else
        {
            return ConfigUtils.getEnum( config, "audio-format", AudioFormat.DEFAULT_FORMAT ).getFriendlyName();
        }
    }



    @Override
    public View getView()
    {
        return view;
    }
}
